package com.graduate.controller;

import com.graduate.exception.BizException;
import com.graduate.service.AsyncService;
import com.graduate.utils.BaseServiceUtils;
import com.graduate.utils.RespBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * 基础测试Controller
 *
 * @createtime 2019-12-28
 * @creator jingchu
 */

@RestController
//@Controller(配合ResponseBody使用)
@ConfigurationProperties(prefix = "image")//配置信息从配置文件读取信息
@EnableAutoConfiguration
public class IndexController extends BaseServiceUtils {

    //@Value("${image.url}")
    private String url;
    //@Value("${image.name}")
    private String name;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }


    //@ResponseBody
    public String first() {
        logger.info("《---------url:" + url + "---------》");
        logger.info("《---------name:" + name + "---------》");
        logger.info("《---------这是一条info日志1---------》");
        return "Hello World!!   你好";
    }


    //异常
    @RequestMapping(value = "/text2", method = RequestMethod.GET)
    @Transactional
    //@ResponseBody
    public RespBody MyException1() {
        int a = 8 / 0;
        return RespBody.success();
    }

    @RequestMapping(value = "/text3", method = RequestMethod.GET)
    @Transactional
    //@ResponseBody
    public RespBody MyException2() {
        if (true) {
            throw new BizException("-1", "未知名错误");
        }
        return RespBody.success();
    }

    @RequestMapping(value = "/text1", method = RequestMethod.GET)
    @Transactional
    //@ResponseBody
    public RespBody MyException3() {
        int a = Integer.parseInt("asd123");
        return RespBody.success();
    }

    @Autowired
    private AsyncService asyncService;

    @RequestMapping("/aysnc")
    //@ResponseBody
    public RespBody AysncTest() throws Exception {
        long start = System.currentTimeMillis();
        Future<String> task1 = asyncService.Task1();
        Future<String> task2 = asyncService.Task2();
        Future<String> task3 = asyncService.Task3();
        while (true) {
            if (task1.isDone() && task2.isDone() && task3.isDone()) {
                break;
            }
            Thread.sleep(1000);
        }
        long end = System.currentTimeMillis();
        logger.info(String.format("所有任务执行完的时间:%d", end - start));
        return RespBody.success(end - start);
    }


    //redis实例
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/redis/text")
    public void test01(){
        stringRedisTemplate.opsForValue().append("msg","hello");

        //读取数据
        String msg = stringRedisTemplate.opsForValue().get("msg");
        System.out.println(msg);
        //list存储数据　
        stringRedisTemplate.opsForList().leftPush("mylist","1");
        stringRedisTemplate.opsForList().leftPush("mylist","2");
        stringRedisTemplate.opsForList().leftPush("mylist","3");
        String mylist = stringRedisTemplate.opsForList().leftPop("mylist"); //删除并查询最顶层的list数据
        System.out.println(mylist);
    }

}

